enyo.kind({
    name: "MyApps.PointOnMap",
    kind: enyo.VFlexBox,
    events: {
        onBack: "",
    },
    components: [    /**
     * Services
     */
    {
        name: "getPosition",
        kind: "PalmService",
        service: "palm://com.palm.location/",
        method: "getCurrentPosition",
        onSuccess: "gotPosition",
        onFailure: "gotPositionFailure",
        onResponse: "gotResponse",
        subscribe: true
    }, {
        name: "getRoute",
        kind: "WebService",
        onSuccess: "gotRoute",
        onFailure: "gotRouteFailure",
    },    /**
     * Layout
     */
    {
        name: "pane",
        kind: "Pane",
        flex: 1,
        components: [{
            name: "golfViewMap",
            kind: "VFlexBox",
            components: [{
                kind: "PageHeader",
                className: "enyo-header-dark",
                components: [{
                    kind: "IconButton",
                    onclick: "doBack",
                    icon: "images/menu-icon-back.png",
                    className: "enyo-button-dark"
                }, {
                    kind: "VFlexBox",
                    flex: 1,
                    align: "center",
                    components: [{
                        name: "title",
                        content: ""
                    }, ]
                }, {
                    kind: "Button",
                    onclick: "showRoute",
                    className: "enyo-button-dark",
                    caption: "Itinéraire"
                }, ]
            }, {
                kind: "Scroller",
                flex: 1,
                components: [{
                    name: "map",
                    kind: "Map",
                    zoom: 10,
                    credentials: "AsuRnAEKCnBbZ8CpIw7cJZqHeVjZSvpKbJedQzy0Th1Cr_BxhHvK8eL4-zQsiP_k",
                }]
            }, ]
        }]
    }, ],
    
    
    create: function(){
        this.inherited(arguments);
        this.position = [];
        this.$.getPosition.call();
        this.latitude;
        this.longitude;
    },
    
    
    gotPosition: function(inSender, inResponse){
        this.position = inResponse;
        console.log("gps success ");
    },
    
    gotPositionFailure: function(inSender, inResponse){
        console.log("gps failure ");
    },
    
    showOnMap: function(latitude, longitude, title){
        //coordinates of point which should be displayed(club,shop)
        this.latitude = latitude;
        this.longitude = longitude;
        
        this.$.title.setContent(title);
        this.$.map.setCenter(latitude, longitude);
        var bingmap = this.$.map.hasMap();
        bingmap.entities.clear();
        var pushpin = new Microsoft.Maps.Pushpin(bingmap.getCenter(), null);
        bingmap.entities.push(pushpin);
        
    },
    
    showRoute: function(){
    
        var start = this.position.latitude + "," + this.position.longitude;
        var end = this.latitude + "," + this.longitude;
        
        var routeRequest = 'http://dev.virtualearth.net/REST/v1/Routes?wp.0=' + start + '&wp.1=' + end + '&routePathOutput=Points&output=json&key=AsuRnAEKCnBbZ8CpIw7cJZqHeVjZSvpKbJedQzy0Th1Cr_BxhHvK8eL4-zQsiP_k';
        this.$.getRoute.setUrl(routeRequest);
        this.$.getRoute.call();
    },
    
    gotRoute: function(inSender, inResponse){
        console.log("route success ");
        this.route = inResponse;
        var bingmap = this.$.map.hasMap();
        
        var pushpinOptions = {
            icon: 'images/poi_custom.png'
        };
        //display pin at my position(start)
        var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(this.position.latitude, this.position.longitude), pushpinOptions);
        bingmap.entities.push(pushpin);
        
        //route points        
        var points = new Array();
        var coordinates = this.route.resourceSets[0].resources[0].routePath.line.coordinates;
        
        for (x in coordinates) {
            points[x] = new Microsoft.Maps.Location(coordinates[x][0], coordinates[x][1]);
            console.log(coordinates[x][0] + "," + coordinates[x][1]);
        }
        
        var polyline = new Microsoft.Maps.Polyline(points, null);
        bingmap.entities.push(polyline);
    },
    
    gotRouteFailure: function(inSender, inResponse){
        console.log("route failure ");
    },

});



